#import "PxCustomPickerView.h"

@implementation PxCustomPickerView {
    int index;
    int min;
}

#pragma mark - Init
- (id)init
{
    self = [super init];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (void)initialize
{
    min = 0;
    index = 0;
    _plusBtn = [UIButton new];
    _minusBtn = [UIButton new];
    _textField = [PxTextField new];
    _textField.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_plusBtn];
    [self addSubview:_minusBtn];
    [self addSubview:_textField];
    
    self.layer.borderWidth = 1;
    self.layer.borderColor = [UIColor colorWithHTMLStr:@"cccccc"].CGColor;
    self.layer.cornerRadius = 3;
    self.layer.masksToBounds = YES;
    
    [_plusBtn setSelector:@selector(plus) withTarget:self];
    [_plusBtn setLongSelector:@selector(plusMax) withTarget:self];
    [_minusBtn setSelector:@selector(minus) withTarget:self];
    [_minusBtn setLongSelector:@selector(minusMin) withTarget:self];
    _textField.delegate = self;
    _textField.keyboardType = UIKeyboardTypePhonePad;
    
    [_plusBtn setImage:[UIImage imageNamed:@"Images/iconAdd"]];
    [_minusBtn setImage:[UIImage imageNamed:@"Images/iconPlus"]];
    _plusBtn.backgroundColor = [UIColor colorWithHTMLStr:@"e3e3e3"];
    _minusBtn.backgroundColor = [UIColor colorWithHTMLStr:@"e3e3e3"];
    _textField.textColor = [UIColor colorWithHTMLStr:@"4c4c4c"];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.equalTo(_minusBtn);
    }];
    
    [_minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(_textField);
        make.top.equalTo(_textField.mas_bottom);
        make.bottom.equalTo(self);
        make.left.equalTo(self);
        make.width.equalTo(_plusBtn);
    }];
    
    [_plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_minusBtn);
        make.bottom.equalTo(self);
        make.right.equalTo(self);
        make.left.mas_equalTo(_minusBtn.mas_right).with.offset(1);
        make.top.equalTo(_textField.mas_bottom);
    }];
}

-(void)setData:(NSMutableArray *)arrays withMinIndex:(int)row;
{
    min = row;
    self.dataSource = arrays;
    
    [self resetValue];
}

-(void)resetValue
{
    index = min;
    [self setTextFieldValue];
}

-(void)plusValue
{
    [self plus];
}

-(void)plus
{
    [self.window endEditing:YES];
    index++;
    [self setTextFieldValue];
}

-(void)plusMax
{
    index = (int)self.dataSource.count-1;
    [self setTextFieldValue];
}

-(void)minus
{
    [self.window endEditing:YES];
    index--;
    [self setTextFieldValue];
}

-(void)minusMin
{
    index = min;
    [self setTextFieldValue];
}

-(void)setTextFieldValue
{
    if (index < min)
        index = min;
    if (index > self.dataSource.count)
        index = (int)self.dataSource.count  - 1;
    _textField.text = self.dataSource[index];
    if (_delegate)
        [_delegate dataChange:(int)self.tag];
}

-(NSString *)getValue
{
    return [NSString stringWithFormat:@"%.2d", index];
}

-(void)setValue:(NSString *)areaCode
{
    index = [areaCode intValue];
    [self setTextFieldValue];
}

# pragma textfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//    index = index + min;
    textField.text = [NSString stringWithFormat:@"%i", index];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    index = [textField.text intValue];
//    index = index - min;
    [self setTextFieldValue];
}

@end